package com.techwells.learningkites.controller;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.techwells.learningkites.R;
import com.techwells.learningkites.fragment.RegisterMobileFragment;
import com.techwells.learningkites.fragment.VideoFragment;
import com.techwells.learningkites.model.RegisterViewModel;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.taco.base.CommonBaseActivity;
import com.techwells.taco.base.CommonBaseFragmentActivity;
import com.techwells.taco.mvvm.Route;
import com.techwells.taco.tasktool.TaskToken;
import com.techwells.taco.utils.ToastUtils;

public class RegisterAcitivity extends CommonBaseFragmentActivity {
	
	private EditText username,password;
	private TextView btn_register;
	
	private RadioGroup rgRegister;
	private RadioButton rbRegisterMail,rbRegisterMobile;
	private Fragment[] mFragments;
	private FragmentManager mFragmentManager;
	private FragmentTransaction mFragmentTransaction;
	
	private RegisterViewModel presentModel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initTitleBar(R.string.register, defaultLeftClickListener);
		initViews();
		initFragments();
	}

	private void initFragments() {
		mFragments = new Fragment[2];
		mFragmentManager = getSupportFragmentManager();
		mFragments[0] = mFragmentManager.findFragmentById(R.id.fragment_register_mail);
		mFragments[1] = mFragmentManager.findFragmentById(R.id.fragment_register_mobile);
		mFragmentTransaction = mFragmentManager.beginTransaction().hide(mFragments[0])
				.hide(mFragments[1]);
		mFragmentTransaction.show(mFragments[1]).commit();
		rbRegisterMobile.setChecked(true);
		rgRegister.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				mFragmentTransaction = mFragmentManager.beginTransaction().hide(mFragments[0])
						.hide(mFragments[1]);
				switch (checkedId) {
				case R.id.tab_rb_mailregister:
//					mFragmentTransaction.show(mFragments[0]).commit();
					break;
				case R.id.tab_rb_mobileregister:
					mFragmentTransaction.show(mFragments[1]).commit();
					break;
				default:
					break;
				}
			}
		});
		
	}

	private void initViews() {
		rgRegister = (RadioGroup) findViewById(R.id.register_rg);
		rbRegisterMail = (RadioButton) findViewById(R.id.tab_rb_mailregister);
		rbRegisterMobile = (RadioButton) findViewById(R.id.tab_rb_mobileregister);
		
	}

	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();
		presentModel = (RegisterViewModel) baseViewModel;
	}
	
	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);
		
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_CAPTCHA) || method.equals(KitesServiceMediator.SERVICE_DO_REGISTER)
				|| method.equals(KitesServiceMediator.SERVICE_DO_LOGIN)) {
			RegisterMobileFragment fragment = (RegisterMobileFragment) mFragments[1];
			fragment.refreshData(token);
		}
	}

	@Override
	public void requestFailedHandle(TaskToken token, int errorCode,
			String errorMsg) {
		// TODO Auto-generated method stub
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_DO_REGISTER)) {
			switch (errorCode) {
			case 469:
				ToastUtils.show(this, "用户名已存在");
				break;
			case 461:
				ToastUtils.show(this, "验证码错误");
				break;
			case 460:
				ToastUtils.show(this, "验证码失效");
				break;
			case 400:
				ToastUtils.show(this, "请输入验证码");
				break;

			default:
				ToastUtils.show(this, errorMsg);
				break;
			}
		}
		
	}
	

}
